package com.qiaofang.repository;

import com.qiaofang.entity.OtherCompany;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface OtherCompanyRepository extends JpaRepository<OtherCompany, Long> {

    @Query(value = "from OtherCompany o where o.channelCompanyId in (:companyIds) and o.isDeleted = '0' order by o.id asc ")
    List<OtherCompany> listOtherCompany(@Param(value = "companyIds") List<Long> companyIds);

    @Transactional
    @Modifying
    @Query(value = "update OtherCompany set isDeleted = 1 where channelCompanyId = :companyId")
    void deleteOtherCompany(@Param(value = "companyId")Long companyId);

    @Query(value = "from OtherCompany o where o.isDeleted = 0 and o.channelCompanyId = :companyId")
    OtherCompany getOtherCompanyByChannelCompanyId(@Param(value = "companyId") Long companyId);

    @Query(value = "from OtherCompany o where o.isDeleted = 0 and o.companyUuid = :companyUuid and o.channelCompanyId = :channelCompanyId")
    OtherCompany getOtherCompanyByUuid(@Param(value = "companyUuid") String companyUuid,@Param(value = "channelCompanyId") Long channelCompanyId);

    /**
     * 通过公司uuid 和 渠道公司id 共同查询外部关联关系(包括已删除的)
     * @param companyUuid
     * @param channelCompanyId
     * @return
     */
    @Query(value = "from OtherCompany o where o.companyUuid = :companyUuid and o.channelCompanyId = :channelCompanyId")
    List<OtherCompany> listOtherCompanyByUuid(@Param(value = "companyUuid") String companyUuid,@Param(value = "channelCompanyId") Long channelCompanyId);

    /**
     * 同过公司uuid查询 外部关联关系
     * @param companyUuid
     * @return
     */
    @Query(value = "from OtherCompany o where o.isDeleted = 0 and o.companyUuid = :companyUuid")
    OtherCompany getOtherCompany(@Param(value = "companyUuid") String companyUuid);

}
